package com.example.xunai.dto.response.conversation;

import com.example.xunai.entity.ConversationMessage;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MessageResponse {
    private Long id;
    private Long conversationId;
    private Integer turn;
    private String role;
    private String content;
    private LocalDateTime createTime;
    private Integer tokenCount;
    private String modelType;

    public static MessageResponse fromEntity(ConversationMessage message) {
        return MessageResponse.builder()
                .id(message.getId())
                .conversationId(message.getConversationId())
                .turn(message.getTurn())
                .role(message.getRole().name())
                .content(message.getContent())
                .createTime(message.getCreateTime())
                .build();
    }

    public static MessageResponse fromEntity(ConversationMessage message, Integer tokenCount, String modelType) {
        return MessageResponse.builder()
                .id(message.getId())
                .conversationId(message.getConversationId())
                .turn(message.getTurn())
                .role(message.getRole().name())
                .content(message.getContent())
                .createTime(message.getCreateTime())
                .tokenCount(tokenCount)
                .modelType(modelType)
                .build();
    }

    public boolean isUserMessage() {
        return "user".equals(role);
    }

    public boolean isAssistantMessage() {
        return "assistant".equals(role);
    }
}